package org.asiainfo.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.asiainfo.common.idempotent.annotation.RepeatSubmit;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.system.service.SysConfigService;
import org.asiainfo.system.domain.vo.SysConfigVo;
import org.asiainfo.system.domain.bo.SysConfigBo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import jakarta.validation.constraints.*;
import org.asiainfo.common.core.validate.AddGroup;
import org.asiainfo.common.core.validate.EditGroup;

import java.util.List;

/**
 * 参数配置表
 *
 * @author dotor-ww
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/config")
public class SysConfigController extends BaseController {

    private final SysConfigService sysConfigService;

    /**
     * 分页查询
     *
     * @param sysConfigBo 筛选条件
     * @param pageQuery   分页对象
     * @return 查询结果
     */
    @SaCheckPermission("system:config:list")
    @GetMapping("/list")
    public TableDataInfo<SysConfigVo> list(SysConfigBo sysConfigBo, PageQuery pageQuery) {
        return sysConfigService.queryPageList(sysConfigBo, pageQuery);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param configId 主键
     * @return 单条数据
     */
    @SaCheckPermission("system:config:query")
    @GetMapping("/{configId}")
    public R<SysConfigVo> getInfo(@NotNull(message = "配置ID不能为空") @PathVariable("configId") Long configId) {
        return R.ok(sysConfigService.queryById(configId));
    }

    /**
     * 新增数据
     *
     * @param sysConfigBo 实体
     * @return 新增结果
     */
    @SaCheckPermission("system:config:add")
    @Log(title = "参数配置", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysConfigBo sysConfigBo) {
        sysConfigService.insertByBo(sysConfigBo);
        return R.ok();
    }

    /**
     * 编辑数据
     *
     * @param sysConfigBo 实体
     * @return 编辑结果
     */
    @SaCheckPermission("system:config:edit")
    @Log(title = "参数配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysConfigBo sysConfigBo) {
        sysConfigService.updateByBo(sysConfigBo);
        return R.ok();
    }

    /**
     * 删除数据
     *
     * @param configIds 主键
     * @return 删除是否成功
     */
    @SaCheckPermission("system:config:remove")
    @Log(title = "参数配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public R<Void> remove(@NotNull(message = "配置ID不能为空") @PathVariable Long[] configIds) {
        return toAjax(sysConfigService.deleteWithValidByIds(List.of(configIds)));
    }

    /**
     * 根据key查询配置
     *
     * @param configKey 参数key
     * @return 查询结果
     */
    @SaCheckPermission("system:config:query")
    @GetMapping("/configKey/{configKey}")
    public R<Object> getConfigByKey(@NotBlank(message = "参数键不能为空") @PathVariable String configKey) {
        return R.ok(sysConfigService.selectConfigByKey(configKey));
    }

    /**
     * 刷新配置缓存
     *
     * @return 刷新结果
     */
    @SaCheckPermission("system:config:remove")
    @Log(title = "参数配置", businessType = BusinessType.CLEAN)
    @PostMapping("/refreshCache")
    public R<Void> refreshCache() {
        sysConfigService.resetConfigCache();
        return R.ok();
    }
}

